package com.example.news.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.news.MainActivity;
import com.example.news.R;
import com.example.news.adapter.OneAdapter;
import com.example.news.bean.OneBean;
import com.example.news.iteam.Iteam_oneActivity;
import com.example.news.sql.Sql;

import java.util.ArrayList;

public class ShouchangFragment extends Fragment {
    private ListView one_listView;
    private ArrayList list1;
    private ArrayList<OneBean>list;
    private ArrayList<OneBean>aa;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_changshang, container, false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        list1=new ArrayList();
        list=new ArrayList<>();
        aa=new ArrayList<>();


        init();
        one_listView=getActivity().findViewById(R.id.changshang_listView);

        Sql sql=new Sql(getActivity());//数据库
        SQLiteDatabase sqLiteDatabase=sql.getReadableDatabase();
        Cursor cursor=null;
      try{
       cursor = sqLiteDatabase.query("infor",null,"name=?",new String[]{((MainActivity)getActivity()).aa()},null,null,null);
       //   将获取到的用户名到数据库中去查询
       if (cursor.getCount()!=0&&cursor!=null){//判段查询数据
              while (cursor.moveToNext()){
                  list1.add(cursor.getString(cursor.getColumnIndex("i")));//获取标识,并保存到集合中
              }
          }
          for (int  i=0;i<list1.size();i++){//遍历标识
              for (int j=0;j<list.size();j++){//遍历数据
                  if (list1.get(i).toString().equals(String.valueOf(list.get(j).getIndex()))){//与数据中的标识进行比对
                    //  Log.e("asd",list1.get(i).toString());
                      aa.add(list.get(j));//比对符合添加到集合中
                  }
              }
          }
          one_listView.setAdapter(new OneAdapter(aa,getActivity()));//将数据加载到适配器中

      }catch (Exception e){

      }

        one_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(getActivity(), Iteam_oneActivity.class);
                intent.putExtra("i", ShouchangFragment.this.list.get(i).getIndex()+"");
                intent.putExtra("name", ShouchangFragment.this.list.get(i).getName());
                startActivity(intent);
                //Toast.makeText(getActivity(), ""+list.get(i).getIndex(),Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void init() {
        Fragment_one fragment_one=((MainActivity) getActivity()).getFragment_ont();
        list=fragment_one.getList();

    }
}
